package com.my.magicApi.utils;

import org.ssssssss.magicapi.modules.servlet.RequestModule;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 */
public class EncodeUtil {
    /**
     * @param   fileNames
     * @param   request
     * @Description: 导出文件转换文件名称编码
     */
    public static String encodeFileName(String fileNames, RequestModule request) {
        String codedFilename = null;
        try {
            List<String> agents = request.getHeaders("USER-AGENT");
            String agent = agents.get(0);
//            String agent = request.getHeaders("USER-AGENT");
            if (null != agent && agent.contains("MSIE") || null != agent && agent.contains("Trident") || null != agent && agent.contains("Edge")) {// ie浏览器及Edge浏览器
                String name = java.net.URLEncoder.encode(fileNames, "UTF-8");
                codedFilename = name;
            } else if (null != agent && agent.contains("Mozilla")) {
                // 火狐,Chrome等浏览器
                codedFilename = new String(fileNames.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            }else{
                codedFilename  = fileNames;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return codedFilename;
    }
}
